package util;

import java.util.ArrayList;
import java.util.List;

public class LoopTest {
	public static void main(String[] args) {
		List<String> source = new ArrayList<String>();
		List<String> target = new ArrayList<String>();
		source.add("aaa");
		source.add("ggg");
		source.add("kkk");
		source.add("eee");
		source.add("fff");
		
		target.add("kkk");
		target.add("eee");
		
		int index = indexOfSubList(source, target);
		System.out.println(index);
		
	}
	
	public static int indexOfSubList(List<?> source, List<?> target){
		int sourceSize = source.size();
        int targetSize = target.size();
        int maxCandidate = sourceSize - targetSize;
		
		nextCand:
            for (int candidate = 0; candidate <= maxCandidate; candidate++) {
                for (int i=0, j=candidate; i<targetSize; i++, j++)
                    if (!eq(target.get(i), source.get(j)))
                        continue nextCand;  // Element mismatch, try next cand
                return candidate;  // All elements of candidate matched target
            }
        return -1;
	}
	
	static boolean eq(Object o1, Object o2) {
        return o1==null ? o2==null : o1.equals(o2);
    }
	
}
